﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using mshtml;
using Nissen_Model;
using System.Xml;
using PosHacker.Form;
using System.Security.Permissions;

namespace Nissen_Marking.Forms
{
    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public partial class FormBrowser : Form
    {

        //Save UnitNo Select result and Submit
        public PosHacker.Form.FormSetScoringNo vFormSetScoringNo { get; set; }

        public FormPartrailQuestion MainForm { get; set; }

        private bool isSubmit = false;

        private bool isFirst = true;//init get html document(webbrowser)

        public int formIndex { get; set; }

        private int SpaceKeyTimes = 0;

        private FormAnswerPaper _formAnswerPaper;
        
        public FormBrowser(string vLink)
        {
            InitializeComponent();

            this.webBrowser1.Url = new Uri(vLink);

            this._formAnswerPaper = new FormAnswerPaper();
        }

        private void FormBrowser_Load(object sender, EventArgs e)
        {
            this.webBrowser1.IsWebBrowserContextMenuEnabled = false;
            this.webBrowser1.WebBrowserShortcutsEnabled = false;
            this.KeyPreview = true;
            this.panel1.SetBounds(this.panel1.Location.X, 33, this.panel1.Size.Width, this.Size.Height - 60);
            this.RadioBtnsVisible();
        }

        #region Form View Control
        private void RadioBtnsSelect() {

            switch (this.MainForm.BrowserWindowPosition)
            {
                case 0:
                    this.radioButton1.Checked = true;
                    break;
                case 1:
                    this.radioButton2.Checked = true;
                    break;
                case 2:
                    this.radioButton3.Checked = true;
                    break;
                case 3:
                    this.radioButton4.Checked = true;
                    break;
                case 4:
                    this.radioButton5.Checked = true;
                    break;
                default:
                    break;
            }
        }

        public void RadioBtnsText()
        {

            if (this.MainForm.ScoringNos.Length==1)
            this.radioButton1.Text = "题" + this.MainForm.ScoringNos[0];
            if (this.MainForm.ScoringNos.Length == 2)
            this.radioButton2.Text = "题" + this.MainForm.ScoringNos[1];
            if (this.MainForm.ScoringNos.Length == 3)
            this.radioButton3.Text = "题" + this.MainForm.ScoringNos[2];
            if (this.MainForm.ScoringNos.Length == 4)
            this.radioButton4.Text = "题" + this.MainForm.ScoringNos[3];
            if (this.MainForm.ScoringNos.Length == 5)
            this.radioButton5.Text = "题" + this.MainForm.ScoringNos[4];
        }

        private void RadioBtnsVisible() { 
                this.radioButton1.Visible = false;
                this.radioButton2.Visible = false;
                this.radioButton3.Visible = false;
                this.radioButton4.Visible = false;
                this.radioButton5.Visible = false;
            if (!this.checkBoxAutoNext.Checked)
            { 

                for(int i = 1; i<=this.MainForm.FormsCount;i++){
                    switch(i){
                        case 1:
                            this.radioButton1.Visible = true;
                            
                            break;
                        case 2:
                            this.radioButton2.Visible = true;
                            
                            break;
                        case 3:
                            this.radioButton3.Visible = true;
                            
                            break;
                        case 4:
                            this.radioButton4.Visible = true;
                            
                            break;
                        case 5:
                            this.radioButton5.Visible = true;
                            
                            break;
                        default:
                            break;
                    }

                }
            }
        }
        #endregion

        #region Web Browser
        //Get Questions 
        private void SubmitForMark() {//应该只修改选题的那几项，保证延续性
            this.Enabled = false;
            if (this.webBrowser1.Document.Forms.Count != 0)
            {

                HtmlElementCollection hec;
                HtmlElement he;
                hec =
                this.webBrowser1.Document.Forms[0].GetElementsByTagName("input");

                mshtml.HTMLDocument hd =
                    (HTMLDocument)this.webBrowser1.Document.DomDocument;
                mshtml.HTMLFormElement hfe =
                (HTMLFormElement)hd.forms.item(0);

                string vName = "";
                string vValue = "";
                Type vType =
                    vFormSetScoringNo.GetType();
                System.Reflection.PropertyInfo vPropertyInfo;
                Console.WriteLine("++++++++++++++++++++++++++++++++++");
                for (int i = 0; i < hec.Count; i++)
                {
                    he = hec[i];

                    vName =
                    he.GetAttribute("name");

                    vPropertyInfo =
                    vType.GetProperty(vName);

                    Console.WriteLine(vName);
                    Console.WriteLine("&");
                    if (vPropertyInfo != null)
                    {
                        vValue =
                        vPropertyInfo.GetValue(vFormSetScoringNo, null).ToString();
                        //
                        he.SetAttribute("value", vValue);
                        Console.WriteLine(vValue);
                    }
                }
                hfe.submit();
                Console.WriteLine("---------------------------------------");
            }
        }

        private void PageControl() {

            HTMLDocument ms_hd =
                (HTMLDocument)this.webBrowser1.Document.DomDocument;

            ms_hd.parentWindow.execScript(
    "function scall(){"
    //+ "alert(document.body.scrollTop);"
        + "document.getElementById(\"LayerTitle\").style.top = (document.body.scrollTop) + \"px\";"
        //+ "document.getElementById(\"LayerTitle\").style.left = (document.body.scrollLeft) + \"px\";"

        + "document.getElementById(\"questionSettingLayer\").style.top = (document.body.scrollTop + 55) + \"px\";"
        //+ "document.getElementById(\"questionSettingLayer\").style.left = (document.documentElement.scrollLeft) + \"px\";"

        + "document.getElementById(\"markingTool\").style.top = (document.body.scrollTop + 210) + \"px\";"
        //+ "document.getElementById(\"markingTool\").style.left = (document.body.scrollLeft) + \"px\";"

        + "document.getElementById(\"correctAnswer\").style.top = (document.body.scrollTop + 180) + \"px\";"
        //+ "document.getElementById(\"markingTool\").style.left = (document.body.scrollLeft) + \"px\";"

        + "}"
    );
            ms_hd.parentWindow.execScript("window.onscroll=scall;");
            ms_hd.parentWindow.execScript("window.onresize=scall;");
            //ms_hd.parentWindow.execScript("window.onload=scall;");



            ms_hd.parentWindow.execScript(
            "function CheckAllMarkButton(){"

                + "CHECK_MARU = false;"
                + "CHECK_BATU = false;"
                + "CHECK_MUKAITOU = false;"

                + "var tmpLength = document.getElementById(\"markingSpace\").children.length;"

                + "var tmpValue;"
                + "for(i = 0;i < tmpLength;i++){"

                + "tmpValue = document.getElementById(\"markerLayer_\"+i).className;"

                + "if(tmpValue == \"maru\"){CHECK_MARU = true;}"
                + "if(tmpValue == \"batu\"){CHECK_BATU = true;}"
                + "if(tmpValue == \"mukaitou\"){CHECK_MUKAITOU = true;}"
                + "}"

             + "var tmpValue1 = document.getElementById(\"allmaru\");"
             + "if(CHECK_MARU){"
             + "tmpValue1.disabled = true;"
             + "}else{"
             + "tmpValue1.disabled = false;}"

             + "var tmpValue2 = document.getElementById(\"allbatu\");"
             + "if(CHECK_BATU){"
             + "tmpValue2.disabled = true;"
             + "}else{"
             + "tmpValue2.disabled = false;}"

             + "var tmpValue3 = document.getElementById(\"allmukaito\");"
             + "if(CHECK_MUKAITOU){"
             + "tmpValue3.disabled = true;"
             + "}else{"
             + "tmpValue3.disabled = false;}"

             + "if(CHECK_MARU&&CHECK_BATU&&CHECK_MUKAITOU){"
             + "tmpValue1.disabled = false;"
             + "tmpValue2.disabled = false;"
             + "tmpValue3.disabled = false;}}"
             );


            //markingTool

            //ms_hd.body.setAttribute("onContextMenu","'return false;'");
            //this.webBrowser1.Document.Body.SetAttribute("onContextMenu", "self.event.returnValue=false;");
            //this.webBrowser1.Refresh();

            HtmlElement he;
            //div "markingSpace"
            he = this.webBrowser1.Document.GetElementById("markingSpace");
            he.OuterHtml = he.OuterHtml.Replace("<DIV", "<DIV onclick=\"CheckAllMarkButton();\"");
            //he.SetAttribute("onclick", "alert(1);");

            //back to menu
            he = this.webBrowser1.Document.GetElementById("backToMenuBtn");
            he.OuterHtml = this.DivHidden(he.OuterHtml);

            //Mark Confirmed
            he = this.webBrowser1.Document.GetElementById("submitMarkBtn");
            he.OuterHtml = this.DivHidden(he.OuterHtml);

            //Setting block
            he = this.webBrowser1.Document.Forms[0];
            he.OuterHtml = this.DivHidden(he.OuterHtml);

            //init click display button
            HTMLButtonElement hbe =
                (HTMLButtonElement)ms_hd.getElementById("showanswer");
            if (hbe != null)
                hbe.click();

        }

        //record mark info
        private void LogMarkInfo(string pResponse)
        {

            List<string[]> lResponses = null;// = new List<string[]>;

            string strResponsePre = pResponse;


            if (!"".Equals(strResponsePre))
                lResponses = this.ParseResponseXML(strResponsePre);
            else
                return;

            //Log
            PartrailQuestionService pqs = new PartrailQuestionService();

            List<RecordData> lRD = new List<RecordData>();
            RecordData rd;
            String strQuestionNo = "";
            foreach (string[] vResponse in lResponses)
            {
                rd = new RecordData();

                strQuestionNo = vResponse[1]
                                    + "_"
                                    + Nissen_Common.ContextManager.Instance._FormSetCourse.subjectCourseTestId
                                    + "_"
                                    + vResponse[0];

                rd.QuestionNo = strQuestionNo;
                rd.WorkerNo = Nissen_Common.ContextManager.Instance.CurrentUser.LoginSSO;
                rd.NissenSSO = Nissen_Common.ContextManager.Instance.CurrentUser.NissenSSO.NissenSSOCode;
                //rd.MachineCode = Nissen_Common.ContextManager.Instance.CurrentUser.Machine.MachineName;
                string strSymbol = "";
                switch (vResponse[2])
                {
                    case "good":
                        strSymbol = "1";
                        break;
                    case "bad":
                        strSymbol = "2";
                        break;
                    case "none":
                        //strSymbol = "4";
                        strSymbol = "3";
                        break;
                    case "partial":
                        strSymbol = "4";
                        break;
                    case "abnormal":
                        strSymbol = "5";
                        break;

                    default:
                        break;
                }
                rd.QuestionResult = strSymbol;//symbol to code

                rd.FirstWorkDate = DateTime.Now;
                rd.EndWorkDate = DateTime.Now;

                rd.MachineCode = "";

                lRD.Add(rd);
            }
            pqs.SubmitMarkableQuestions(lRD);

        } 
        private void LogMarkInfo() {

            List<string[]> lResponses = null;// = new List<string[]>;

            string strResponsePre = "";
            HtmlElement he =
            this.webBrowser1.Document.Forms[0];

            if (he != null)
                he = he.Children[3];

            if (he != null)
                strResponsePre = he.GetAttribute("value");

            if (!"".Equals(strResponsePre))
                lResponses = this.ParseResponseXML(strResponsePre);
            else
                return;

            //Log
            PartrailQuestionService pqs = new PartrailQuestionService();

            List<RecordData> lRD = new List<RecordData>();
            RecordData rd;
            String strQuestionNo = "";
            foreach (string[] vResponse in lResponses)
            {
                rd = new RecordData();

                strQuestionNo = vResponse[1] 
                                    + "_" 
                                    + Nissen_Common.ContextManager.Instance._FormSetCourse.subjectCourseTestId 
                                    + "_" 
                                    + vResponse[0];

                rd.QuestionNo = strQuestionNo;
                rd.WorkerNo = Nissen_Common.ContextManager.Instance.CurrentUser.LoginSSO;
                rd.NissenSSO = Nissen_Common.ContextManager.Instance.CurrentUser.NissenSSO.NissenSSOCode;
                //rd.MachineCode = Nissen_Common.ContextManager.Instance.CurrentUser.Machine.MachineName;
                string strSymbol = "";
                switch (vResponse[2])
                {
                    case "good":
                        strSymbol = "1";
                        break;
                    case "bad":
                        strSymbol = "2";
                        break;
                    case "none":
                        //strSymbol = "4";
                        strSymbol = "3";
                        break;
                    case "partial":
                        strSymbol = "4";
                        break;
                    case "abnormal":
                        strSymbol = "5";
                        break;

                    default:
                        break;
                }
                rd.QuestionResult = strSymbol;//symbol to code

                rd.FirstWorkDate = DateTime.Now;
                rd.EndWorkDate = DateTime.Now;

                rd.MachineCode = "";

                lRD.Add(rd);
            }
            pqs.SubmitMarkableQuestions(lRD);

        } 
       
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            //Init Open Link
            if (isFirst)
            {


                this.SubmitForMark();

                this.Show();
                this.Hide();

                this.isFirst = false;
                return;
            }

            FormPartrailQuestion.isGroupSubmit = false;//bug



            this.Enabled = true;

            //redefine page
            this.PageControl();

            //Log submit info
            if (isSubmit)
            {
                HtmlElementCollection hec = 
                this.webBrowser1.Document.GetElementsByTagName("input");
                HtmlElement he = hec["scoringNo"];

                if (!this.vFormSetScoringNo.scoringNo.Equals(he.GetAttribute("value"))) {
                    this.LogMarkInfo(this.docRespose);
                    this.SubmitForMark();
                }

                this.isSubmit = false;
                new System.Threading.Thread(new System.Threading.ThreadStart(LogMarkInfoAgent)).Start();//Thread pcj 12.04.21
                //this.LogMarkInfo();
            }
            //this.Show();
        }
        delegate void dl();
        private void LogMarkInfoAgent() { 
            dl vdl = new dl(LogMarkInfo);
            this.Invoke(vdl);
            if(!this.MainForm.PermissionCheck(this.vFormSetScoringNo.scoringNo))
                this.Invoke(new dl(delegate() { this.Close(); }));
        }
        #endregion

        private string docRespose = "";
        public void SetRespose(string pResponse) {
            this.docRespose = pResponse;
        }

        //submit confirm
        private void button1_Click(object sender, EventArgs e)//submit
        {
            if (isSubmit)
                return;

            HTMLDocument ms_hd=
            (HTMLDocument)this.webBrowser1.Document.DomDocument;
            HTMLButtonElement hbe =
                (HTMLButtonElement)ms_hd.getElementById("submitMarkBtn");

            //this.webBrowser1.Document.InvokeScript();
            //HTMLWindow2Class hw2c =
            //(HTMLWindow2Class)this.webBrowser1.Document.Window.DomWindow;

            if (true.Equals(this.webBrowser1.Document.InvokeScript("checkMarkResult")))
            {
                this.webBrowser1.ObjectForScripting = this;
                ((HTMLWindow2)ms_hd.parentWindow).execScript(
                "function submitToMarkAction() {"
                + "disableButtons(true);"
                + "document.pcPartialMarkingForm.action = \"/MarkingSystemWeb/pcPartialMarking.do\";"
                + "constructResponse();"
                + "var comXml = buildCommentXml(xOriginOffset,yOriginOffset);"
                + "document.pcPartialMarkingForm.commentXml.value = (comXml.xml).replace(\"/(\\r\\n)+$/g\",'');"
                + "window.external.SetRespose(document.pcPartialMarkingForm.response.value);"//important pcj
                + "document.pcPartialMarkingForm.submit();}");

                hbe.click();//submit

            }
            else
                return;

            this.isSubmit = true;

            //show next window
            if(this.checkBoxAutoNext.Checked)
                this.MainForm.ShowNextBrowserWindow();




            return;
        }

        private void buttonSet_Click(object sender, EventArgs e)
        {
            FormPartrailQuestion fpq = new FormPartrailQuestion(this);
            if (fpq.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                //return;
                this.SubmitForMark();
        }

        private void checkBoxAutoNext_CheckedChanged(object sender, EventArgs e)
        {
            this.MainForm.CheckBoxAutoNext = this.checkBoxAutoNext.Checked;
            this.RadioBtnsVisible();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            this.MainForm.ShowNextBrowserWindow(0);
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            this.MainForm.ShowNextBrowserWindow(1);
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            this.MainForm.ShowNextBrowserWindow(2);
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            this.MainForm.ShowNextBrowserWindow(3);
        }

        private void radioButton5_CheckedChanged(object sender, EventArgs e)
        {
            this.MainForm.ShowNextBrowserWindow(4);
        }

        private void FormBrowser_VisibleChanged(object sender, EventArgs e)
        {
            if (Visible)
            {
                this.checkBoxAutoNext.Checked = this.MainForm.CheckBoxAutoNext;
                this.webBrowser1.Focus();
            }
            if (this.Visible && !this.isFirst) {
                this.RadioBtnsSelect();
            }
        }

        private void FormBrowser_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.MainForm.Show();
        }

        private void FormBrowser_KeyDown(object sender, KeyEventArgs e)
        {
            if (this.Focused)
                this.webBrowser1.Focus();

            Console.WriteLine(this.Text);
        }

        #region Common
        //for page control
        private string DivHidden(string strOutHtml)
        {

            return "<div style=\"visibility:hidden\">" + strOutHtml + "</div>";
        }

        private List<string[]> ParseResponseXML(string reqXML) {

            List<string[]> lStrArray = new List<string[]>();
            string[] tmpStrArray;// = new string[4];
            
            XmlDocument xd = new XmlDocument();
            xd.LoadXml(reqXML.ToString());
            XmlNodeList results = xd.GetElementsByTagName("markingPcPartial");
            if (results != null && results.Count > 0)
            {
                XmlNode nodeParent = results.Item(0);
                XmlNodeList childNodes = nodeParent.ChildNodes;

                string questionId = "";

                for (int i = 0; i < childNodes.Count; i++)
                {
                    tmpStrArray = new string[4];
                    Console.WriteLine(childNodes[i].Name);
                    Console.WriteLine(childNodes[i].InnerText);
                    if (childNodes[i].Name == "message")
                    {


                    }
                    else if (childNodes[i].Name == "questionId")
                    {
                        questionId = childNodes[i].InnerText;
                    }
                    else if (childNodes[i].Name == "question")
                    {
                        tmpStrArray[0] = questionId;//questionId
                        tmpStrArray[1] = childNodes[i].ChildNodes[0].FirstChild.Value;//paperRegNo(student)

                        if (childNodes[i].ChildNodes[1].FirstChild == null)
                            continue;
                        tmpStrArray[2] = childNodes[i].ChildNodes[1].FirstChild.Value;//result symbol
                        if (childNodes[i].ChildNodes[2].FirstChild == null)
                            continue;
                        tmpStrArray[3] = childNodes[i].ChildNodes[2].FirstChild.Value;//point

                        lStrArray.Add(tmpStrArray);
                    }

                }
            }
            return lStrArray;
        }

        private List<FormMark> ParsePartialXML(string reqXML)
        {


            List<FormMark> fmList =null;

            XmlDocument xd = new XmlDocument();
            
            xd.LoadXml(System.Web.HttpUtility.UrlDecode(reqXML.ToString()));
            XmlNodeList results = xd.GetElementsByTagName("markingPcPartial");

            if (results != null
                && results.Count > 0
                )
            {

                XmlNode nodeParent = results.Item(0);
                XmlNodeList childNodes = nodeParent.ChildNodes;

                string questionId = "";
                string allocatedPoint = "";

               fmList = new List<FormMark>();

                for (int i = 0; i < childNodes.Count; i++)
                {
                    Console.Write(childNodes[i].Name);
                    if (childNodes[i].Name == "message")
                    {


                    }
                    else if (childNodes[i].Name == "questionId")
                    {
                        string unitNoMarking = childNodes[i].InnerText;
                        questionId = unitNoMarking;

                    }
                    else if (childNodes[i].Name == "allocatedPoint")
                    {
                        //	alert("childNode[" + i + "] " + childNodes[i].tagName + "=" + childNodes[i].childNodes[0].nodeValue);
                        allocatedPoint = childNodes[i].FirstChild.Value;

                    }
                    else if (childNodes[i].Name == "height")
                    {
                        //	alert("childNode[" + i + "] " + childNodes[i].tagName + "=" + childNodes[i].childNodes[0].nodeValue);
                        //CELL_HEIGHT = eval(childNodes[i].childNodes[0].nodeValue);

                    }
                    else if (childNodes[i].Name == "width")
                    {
                        //	alert("childNode[" + i + "] " + childNodes[i].tagName + "=" + childNodes[i].childNodes[0].nodeValue);
                        //CELL_WIDTH = eval(childNodes[i].childNodes[0].nodeValue);

                    }
                    else if (childNodes[i].Name == "markingImageGood")
                    {
                        //	alert("childNode[" + i + "] " + childNodes[i].tagName + "=" + childNodes[i].childNodes[0].nodeValue);
                        //IMAGE_MARKING_TOOL_GOOD = childNodes[i].childNodes[0].nodeValue;

                    }
                    else if (childNodes[i].Name == "markingImageBad")
                    {
                        //	alert("childNode[" + i + "] " + childNodes[i].tagName + "=" + childNodes[i].childNodes[0].nodeValue);
                        //IMAGE_MARKING_TOOL_BAD = childNodes[i].childNodes[0].nodeValue;
                    }
                    else if (childNodes[i].Name == "markingImagePartial")
                    {
                        //	alert("childNode[" + i + "] " + childNodes[i].tagName + "=" + childNodes[i].childNodes[0].nodeValue);
                        //IMAGE_MARKING_TOOL_PARTIAL_GOOD = childNodes[i].childNodes[0].nodeValue;
                    }
                    else if (childNodes[i].Name == "markingImageNone")
                    {
                        //	alert("childNode[" + i + "] " + childNodes[i].tagName + "=" + childNodes[i].childNodes[0].nodeValue);
                        //IMAGE_MARKING_TOOL_NO_ANSWER = childNodes[i].childNodes[0].nodeValue;
                    }
                    else if (childNodes[i].Name == "question")
                    {


                        var id = childNodes[i].Attributes["id"];
                        var regNo = childNodes[i].ChildNodes[0].FirstChild;
                        var image = childNodes[i].ChildNodes[1].FirstChild;
                        var studentNo = childNodes[i].ChildNodes[2].FirstChild;


                        fmList.Add(new FormMark(id.Value, questionId, regNo.Value, allocatedPoint, image.Value, studentNo.Value));
                        Console.WriteLine(" : " + id.Value);
                        Console.WriteLine(" : " + regNo.Value);
                        Console.WriteLine(" : " + image.Value);
                        Console.WriteLine(" : " + studentNo.Value);

                    }

                    if (childNodes[i].Name != "question")
                    {
                        Console.WriteLine(" : " + childNodes[i].InnerText);
                    }




                }
                string tmpStrJson = "'([";

                foreach (FormMark fm in fmList)
                {
                    tmpStrJson += fm.ToJson();
                    tmpStrJson += ",";
                }
                tmpStrJson = tmpStrJson.TrimEnd(',');
                tmpStrJson += "])'";

                /*marks:
                 * checkMarkResult 
                 * createImageFromObject
                 * redrawPartialPointArea
                 */
            }

            //Get Image OK
            //********* http://pos.toshin.com/MarkingSystemWeb/pcPartialImage.do?imgType=partial&imgRegNo=31223686&unitNo=21&&zoom=100
            //Get Image OK


            return fmList;

            //Request URL:http://pos.toshin.com/MarkingSystemWeb/pcPartialList.do
            //Request Method:POST
            //request:INITIAL
            //requestPre:
            //response:
            //responsePre:
            //showAnswer:false
            //zoom:1.0 
            //$50%,100%,200%
            //canelForward:
            //commentXml:
            //testKouseiId:1264302
            //scoringNo:4
            //maxMarks:3
            //numberOfSet:50
            //mouseMarker:2
            //$1:O,2:X,3:P,4:NoAnswer,5:AbnormalData
            //var MODE_MARKING_NO_SELECT = 0;
            //var MODE_MARKING_GOOD = 1;
            //var MODE_MARKING_BAD = 2;
            //var MODE_MARKING_PARTIAL_GOOD = 3;
            //var MODE_MARKING_NO_ANSWER = 4;
            //var MODE_MARKING_ABNORMAL = 5;

            //req
            //<?xml version="1.0" encoding="UTF-8"?><markingPcPartial><message>PC分割採点</message><questionId>4</questionId><allocatedPoint>3</allocatedPoint><height>92</height><width>118</width><markingImageGood>parts/maru.png</markingImageGood><markingImageBad>parts/batsu.png</markingImageBad><markingImagePartial>parts/sankaku.png</markingImagePartial><markingImageNone>parts/mukaito.png</markingImageNone><question id="0"><paperRegNo>31172181</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31172181&amp;unitNo=4</image><studentNo>1552431 </studentNo></question><question id="1"><paperRegNo>31173371</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31173371&amp;unitNo=4</image><studentNo>7682841 </studentNo></question><question id="2"><paperRegNo>31171149</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31171149&amp;unitNo=4</image><studentNo>1442546 </studentNo></question><question id="3"><paperRegNo>31169548</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31169548&amp;unitNo=4</image><studentNo>1520725 </studentNo></question><question id="4"><paperRegNo>31170718</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31170718&amp;unitNo=4</image><studentNo>1630768 </studentNo></question><question id="5"><paperRegNo>31173370</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31173370&amp;unitNo=4</image><studentNo>1638572 </studentNo></question><question id="6"><paperRegNo>31171022</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31171022&amp;unitNo=4</image><studentNo>1644033 </studentNo></question><question id="7"><paperRegNo>31170727</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31170727&amp;unitNo=4</image><studentNo>1663682 </studentNo></question><question id="8"><paperRegNo>31170197</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31170197&amp;unitNo=4</image><studentNo>1618474 </studentNo></question><question id="9"><paperRegNo>31171152</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31171152&amp;unitNo=4</image><studentNo>1352555 </studentNo></question><question id="10"><paperRegNo>31168628</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31168628&amp;unitNo=4</image><studentNo>1409557 </studentNo></question><question id="11"><paperRegNo>31171142</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31171142&amp;unitNo=4</image><studentNo>1473450 </studentNo></question><question id="12"><paperRegNo>31168349</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31168349&amp;unitNo=4</image><studentNo>1724851 </studentNo></question><question id="13"><paperRegNo>31168339</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31168339&amp;unitNo=4</image><studentNo>1443232 </studentNo></question><question id="14"><paperRegNo>31171715</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31171715&amp;unitNo=4</image><studentNo>1660225 </studentNo></question><question id="15"><paperRegNo>31169616</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31169616&amp;unitNo=4</image><studentNo>1659537 </studentNo></question><question id="16"><paperRegNo>31171721</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31171721&amp;unitNo=4</image><studentNo>1648705 </studentNo></question><question id="17"><paperRegNo>31169615</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31169615&amp;unitNo=4</image><studentNo>1686909 </studentNo></question><question id="18"><paperRegNo>31173365</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31173365&amp;unitNo=4</image><studentNo>1558676 </studentNo></question><question id="19"><paperRegNo>31168674</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31168674&amp;unitNo=4</image><studentNo>1705547 </studentNo></question><question id="20"><paperRegNo>31168680</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31168680&amp;unitNo=4</image><studentNo>1473760 </studentNo></question></markingPcPartial>
            //req0
            //<?xml+version="1.0"+encoding="UTF-8"?><markingPcPartial><message>PC分割採点</message><questionId>4</questionId><allocatedPoint>3</allocatedPoint><height>92</height><width>118</width><markingImageGood>parts/maru.png</markingImageGood><markingImageBad>parts/batsu.png</markingImageBad><markingImagePartial>parts/sankaku.png</markingImagePartial><markingImageNone>parts/mukaito.png</markingImageNone><question+id="0"><paperRegNo>31172181</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31172181&amp;unitNo=4</image><studentNo>1552431+</studentNo></question><question+id="1"><paperRegNo>31173371</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31173371&amp;unitNo=4</image><studentNo>7682841+</studentNo></question><question+id="2"><paperRegNo>31171149</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31171149&amp;unitNo=4</image><studentNo>1442546+</studentNo></question><question+id="3"><paperRegNo>31169548</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31169548&amp;unitNo=4</image><studentNo>1520725+</studentNo></question><question+id="4"><paperRegNo>31170718</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31170718&amp;unitNo=4</image><studentNo>1630768+</studentNo></question><question+id="5"><paperRegNo>31173370</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31173370&amp;unitNo=4</image><studentNo>1638572+</studentNo></question><question+id="6"><paperRegNo>31171022</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31171022&amp;unitNo=4</image><studentNo>1644033+</studentNo></question><question+id="7"><paperRegNo>31170727</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31170727&amp;unitNo=4</image><studentNo>1663682+</studentNo></question><question+id="8"><paperRegNo>31170197</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31170197&amp;unitNo=4</image><studentNo>1618474+</studentNo></question><question+id="9"><paperRegNo>31171152</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31171152&amp;unitNo=4</image><studentNo>1352555+</studentNo></question><question+id="10"><paperRegNo>31168628</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31168628&amp;unitNo=4</image><studentNo>1409557+</studentNo></question><question+id="11"><paperRegNo>31171142</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31171142&amp;unitNo=4</image><studentNo>1473450+</studentNo></question><question+id="12"><paperRegNo>31168349</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31168349&amp;unitNo=4</image><studentNo>1724851+</studentNo></question><question+id="13"><paperRegNo>31168339</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31168339&amp;unitNo=4</image><studentNo>1443232+</studentNo></question><question+id="14"><paperRegNo>31171715</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31171715&amp;unitNo=4</image><studentNo>1660225+</studentNo></question><question+id="15"><paperRegNo>31169616</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31169616&amp;unitNo=4</image><studentNo>1659537+</studentNo></question><question+id="16"><paperRegNo>31171721</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31171721&amp;unitNo=4</image><studentNo>1648705+</studentNo></question><question+id="17"><paperRegNo>31169615</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31169615&amp;unitNo=4</image><studentNo>1686909+</studentNo></question><question+id="18"><paperRegNo>31173365</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31173365&amp;unitNo=4</image><studentNo>1558676+</studentNo></question><question+id="19"><paperRegNo>31168674</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31168674&amp;unitNo=4</image><studentNo>1705547+</studentNo></question><question+id="20"><paperRegNo>31168680</paperRegNo><image>pcPartialImage.do?imgType=partial&amp;imgRegNo=31168680&amp;unitNo=4</image><studentNo>1473760+</studentNo></question></markingPcPartial>

            //<form name="pcPartialMarkingForm" method="post" action="/MarkingSystemWeb/pcPartialList.do" onsubmit="return false;">
            //<input type="hidden" name="request" value="%3C%3Fxml+version%3D%221.0%22+encoding%3D%22UTF-8%22%3F%3E%3CmarkingPcPartial%3E%3Cmessage%3EPC%E5%88%86%E5%89%B2%E6%8E%A1%E7%82%B9%3C%2Fmessage%3E%3CquestionId%3E4%3C%2FquestionId%3E%3CallocatedPoint%3E3%3C%2FallocatedPoint%3E%3Cheight%3E92%3C%2Fheight%3E%3Cwidth%3E118%3C%2Fwidth%3E%3CmarkingImageGood%3Eparts%2Fmaru.png%3C%2FmarkingImageGood%3E%3CmarkingImageBad%3Eparts%2Fbatsu.png%3C%2FmarkingImageBad%3E%3CmarkingImagePartial%3Eparts%2Fsankaku.png%3C%2FmarkingImagePartial%3E%3CmarkingImageNone%3Eparts%2Fmukaito.png%3C%2FmarkingImageNone%3E%3Cquestion+id%3D%220%22%3E%3CpaperRegNo%3E31172181%3C%2FpaperRegNo%3E%3Cimage%3EpcPartialImage.do%3FimgType%3Dpartial%26amp%3BimgRegNo%3D31172181%26amp%3BunitNo%3D4%3C%2Fimage%3E%3CstudentNo%3E1552431+%3C%2FstudentNo%3E%3C%2Fquestion%3E%3Cquestion+id%3D%221%22%3E%3CpaperRegNo%3E31173371%3C%2FpaperRegNo%3E%3Cimage%3EpcPartialImage.do%3FimgType%3Dpartial%26amp%3BimgRegNo%3D31173371%26amp%3BunitNo%3D4%3C%2Fimage%3E%3CstudentNo%3E7682841+%3C%2FstudentNo%3E%3C%2Fquestion%3E%3Cquestion+id%3D%222%22%3E%3CpaperRegNo%3E31171149%3C%2FpaperRegNo%3E%3Cimage%3EpcPartialImage.do%3FimgType%3Dpartial%26amp%3BimgRegNo%3D31171149%26amp%3BunitNo%3D4%3C%2Fimage%3E%3CstudentNo%3E1442546+%3C%2FstudentNo%3E%3C%2Fquestion%3E%3Cquestion+id%3D%223%22%3E%3CpaperRegNo%3E31169548%3C%2FpaperRegNo%3E%3Cimage%3EpcPartialImage.do%3FimgType%3Dpartial%26amp%3BimgRegNo%3D31169548%26amp%3BunitNo%3D4%3C%2Fimage%3E%3CstudentNo%3E1520725+%3C%2FstudentNo%3E%3C%2Fquestion%3E%3Cquestion+id%3D%224%22%3E%3CpaperRegNo%3E31170718%3C%2FpaperRegNo%3E%3Cimage%3EpcPartialImage.do%3FimgType%3Dpartial%26amp%3BimgRegNo%3D31170718%26amp%3BunitNo%3D4%3C%2Fimage%3E%3CstudentNo%3E1630768+%3C%2FstudentNo%3E%3C%2Fquestion%3E%3Cquestion+id%3D%225%22%3E%3CpaperRegNo%3E31173370%3C%2FpaperRegNo%3E%3Cimage%3EpcPartialImage.do%3FimgType%3Dpartial%26amp%3BimgRegNo%3D31173370%26amp%3BunitNo%3D4%3C%2Fimage%3E%3CstudentNo%3E1638572+%3C%2FstudentNo%3E%3C%2Fquestion%3E%3Cquestion+id%3D%226%22%3E%3CpaperRegNo%3E31171022%3C%2FpaperRegNo%3E%3Cimage%3EpcPartialImage.do%3FimgType%3Dpartial%26amp%3BimgRegNo%3D31171022%26amp%3BunitNo%3D4%3C%2Fimage%3E%3CstudentNo%3E1644033+%3C%2FstudentNo%3E%3C%2Fquestion%3E%3Cquestion+id%3D%227%22%3E%3CpaperRegNo%3E31170727%3C%2FpaperRegNo%3E%3Cimage%3EpcPartialImage.do%3FimgType%3Dpartial%26amp%3BimgRegNo%3D31170727%26amp%3BunitNo%3D4%3C%2Fimage%3E%3CstudentNo%3E1663682+%3C%2FstudentNo%3E%3C%2Fquestion%3E%3Cquestion+id%3D%228%22%3E%3CpaperRegNo%3E31170197%3C%2FpaperRegNo%3E%3Cimage%3EpcPartialImage.do%3FimgType%3Dpartial%26amp%3BimgRegNo%3D31170197%26amp%3BunitNo%3D4%3C%2Fimage%3E%3CstudentNo%3E1618474+%3C%2FstudentNo%3E%3C%2Fquestion%3E%3Cquestion+id%3D%229%22%3E%3CpaperRegNo%3E31171152%3C%2FpaperRegNo%3E%3Cimage%3EpcPartialImage.do%3FimgType%3Dpartial%26amp%3BimgRegNo%3D31171152%26amp%3BunitNo%3D4%3C%2Fimage%3E%3CstudentNo%3E1352555+%3C%2FstudentNo%3E%3C%2Fquestion%3E%3Cquestion+id%3D%2210%22%3E%3CpaperRegNo%3E31168628%3C%2FpaperRegNo%3E%3Cimage%3EpcPartialImage.do%3FimgType%3Dpartial%26amp%3BimgRegNo%3D31168628%26amp%3BunitNo%3D4%3C%2Fimage%3E%3CstudentNo%3E1409557+%3C%2FstudentNo%3E%3C%2Fquestion%3E%3Cquestion+id%3D%2211%22%3E%3CpaperRegNo%3E31171142%3C%2FpaperRegNo%3E%3Cimage%3EpcPartialImage.do%3FimgType%3Dpartial%26amp%3BimgRegNo%3D31171142%26amp%3BunitNo%3D4%3C%2Fimage%3E%3CstudentNo%3E1473450+%3C%2FstudentNo%3E%3C%2Fquestion%3E%3Cquestion+id%3D%2212%22%3E%3CpaperRegNo%3E31168349%3C%2FpaperRegNo%3E%3Cimage%3EpcPartialImage.do%3FimgType%3Dpartial%26amp%3BimgRegNo%3D31168349%26amp%3BunitNo%3D4%3C%2Fimage%3E%3CstudentNo%3E1724851+%3C%2FstudentNo%3E%3C%2Fquestion%3E%3Cquestion+id%3D%2213%22%3E%3CpaperRegNo%3E31168339%3C%2FpaperRegNo%3E%3Cimage%3EpcPartialImage.do%3FimgType%3Dpartial%26amp%3BimgRegNo%3D31168339%26amp%3BunitNo%3D4%3C%2Fimage%3E%3CstudentNo%3E1443232+%3C%2FstudentNo%3E%3C%2Fquestion%3E%3Cquestion+id%3D%2214%22%3E%3CpaperRegNo%3E31171715%3C%2FpaperRegNo%3E%3Cimage%3EpcPartialImage.do%3FimgType%3Dpartial%26amp%3BimgRegNo%3D31171715%26amp%3BunitNo%3D4%3C%2Fimage%3E%3CstudentNo%3E1660225+%3C%2FstudentNo%3E%3C%2Fquestion%3E%3Cquestion+id%3D%2215%22%3E%3CpaperRegNo%3E31169616%3C%2FpaperRegNo%3E%3Cimage%3EpcPartialImage.do%3FimgType%3Dpartial%26amp%3BimgRegNo%3D31169616%26amp%3BunitNo%3D4%3C%2Fimage%3E%3CstudentNo%3E1659537+%3C%2FstudentNo%3E%3C%2Fquestion%3E%3Cquestion+id%3D%2216%22%3E%3CpaperRegNo%3E31171721%3C%2FpaperRegNo%3E%3Cimage%3EpcPartialImage.do%3FimgType%3Dpartial%26amp%3BimgRegNo%3D31171721%26amp%3BunitNo%3D4%3C%2Fimage%3E%3CstudentNo%3E1648705+%3C%2FstudentNo%3E%3C%2Fquestion%3E%3Cquestion+id%3D%2217%22%3E%3CpaperRegNo%3E31169615%3C%2FpaperRegNo%3E%3Cimage%3EpcPartialImage.do%3FimgType%3Dpartial%26amp%3BimgRegNo%3D31169615%26amp%3BunitNo%3D4%3C%2Fimage%3E%3CstudentNo%3E1686909+%3C%2FstudentNo%3E%3C%2Fquestion%3E%3Cquestion+id%3D%2218%22%3E%3CpaperRegNo%3E31173365%3C%2FpaperRegNo%3E%3Cimage%3EpcPartialImage.do%3FimgType%3Dpartial%26amp%3BimgRegNo%3D31173365%26amp%3BunitNo%3D4%3C%2Fimage%3E%3CstudentNo%3E1558676+%3C%2FstudentNo%3E%3C%2Fquestion%3E%3Cquestion+id%3D%2219%22%3E%3CpaperRegNo%3E31168674%3C%2FpaperRegNo%3E%3Cimage%3EpcPartialImage.do%3FimgType%3Dpartial%26amp%3BimgRegNo%3D31168674%26amp%3BunitNo%3D4%3C%2Fimage%3E%3CstudentNo%3E1705547+%3C%2FstudentNo%3E%3C%2Fquestion%3E%3Cquestion+id%3D%2220%22%3E%3CpaperRegNo%3E31168680%3C%2FpaperRegNo%3E%3Cimage%3EpcPartialImage.do%3FimgType%3Dpartial%26amp%3BimgRegNo%3D31168680%26amp%3BunitNo%3D4%3C%2Fimage%3E%3CstudentNo%3E1473760+%3C%2FstudentNo%3E%3C%2Fquestion%3E%3C%2FmarkingPcPartial%3E">

            //mark = new Mark;
            //mark.index = id;
            //mark.questionId = questionId;
            //mark.paperRegNo = regNo;
            //mark.allocatedPoint = allocatedPoint;
            //mark.image = image;
            //mark.studentNo = studentNo;

            //createMarkingSpace

        }
        #endregion

        private void FormBrowser_Enter(object sender, EventArgs e)
        {
            Console.WriteLine(this.Text + "--Enter");
        }

        private void webBrowser1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            Console.WriteLine(e.KeyValue);
            
            //this.webBrowser1.PreProcessControlMessage(
            int tmpint = 0;
            switch (e.KeyCode) { 
                case Keys.F12:
                    if (e.KeyCode == Keys.F12)
                    {
                        this.Hide();
                        this._formAnswerPaper.SubjectCourseId = 
                            Nissen_Common.ContextManager.Instance._FormSetCourse.subjectCourseTestId;

                        this._formAnswerPaper.PageNum =
                            Nissen_Common.ContextManager.Instance._FormSetCourse.pageNum;
                        this._formAnswerPaper.ShowDialog();
                        this.Show();
                    }
                    break;
                case Keys.D1:
                    tmpint = 1;
                    break;
                case Keys.D2:
                    tmpint = 2;
                    break;
                case Keys.D3:
                    tmpint = 4;
                    break;
                //case Keys.D4:
                //    tmpint = 4;
                //    break;
                case Keys.D5:
                    tmpint = 5;
                    break;
                case Keys.Space:
                    if (this.SpaceKeyTimes == 0)
                    {
                        this.SpaceKeyTimes++;
                        if (!isSubmit)
                            this.button1_Click(null, null);

                    }else
                        this.SpaceKeyTimes=0;
                    
                    break;
                default:
                    break;
            }

            if(tmpint!=0)
                this.webBrowser1.Document.InvokeScript("setMouseMarker", new object[] { tmpint });

            //this.webBrwser1.PreProcessControlMessage(ref new Message());
        }

        private void FormBrowser_Deactivate(object sender, EventArgs e)
        {
            if (this.Visible) {
                this.Focus();
                this.webBrowser1.Focus();
            }
        }
    }
}
